package so.shanku.essential.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.json.JsonMap;
import net.tsz.afinal.json.JsonParseHelper;
import org.json.JSONArray;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.adapter.MyAppointmentAdapter;
import so.shanku.essential.getdata.*;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.Utils;
import striveen.util.used.view.view.XListView;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by xiaozhen on 2016/3/22.
 * 我的预约
 */
public class MyAppointmentActivity extends BaseUIActivity {
	//找到控件ID
	@ViewInject(id = R.id.xlv)
	private XListView xlv;
	@ViewInject(id = R.id.nodata_layout) //无数据
	private LinearLayout nodataLayout;
	private MyAppointmentAdapter myAppointmentAdapter;
	//保存数据
	private ArrayList<JsonMap<String, Object>> datas = new ArrayList<JsonMap<String, Object>>();
	//分页
	private int nPage = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myappointment);
		//设置适配器
		myAppointmentAdapter = new MyAppointmentAdapter(this);
		//Button的监听回调
		myAppointmentAdapter.setItemCompontClickListener(itemCompontClickListener);
		xlv.setAdapter(myAppointmentAdapter);
		xlv.setPullRefreshEnable(true);
		xlv.setPullLoadEnable(true);
		xlv.setXListViewListener(loadMoreListener);
		xlv.setOnItemClickListener(itemClickListener);
		//加载框
		//loadingToast.show();
		//获取网络数据
		//getServerData();

	}

	/**
	 * item 中组建的点击监听
	 */
	private MyAppointmentAdapter.ItemCompontClickListener itemCompontClickListener=new MyAppointmentAdapter.ItemCompontClickListener() {
		@Override
		public void onItemCompontClickListener(int position, int itemType) {
			JsonMap jsonMap=datas.get(position);
			switch (itemType) {
				case Constant.SHARE_IV_CLICK:
						//jumpTo();
					break;
				default:
			}

		}
	};

	private void getServerData() {
		//初始化网络请求参数
		HashMap params = new HashMap();
		params.put("userId", Utils.getUserId(mContext));
		params.put("nPage", nPage + 1);

		//  创建网络请求的对象
		GetDataQueue getDataQueue = new GetDataQueue();
		//存放接口文档给的值
		getDataQueue.setActionName(GetDataConfing.Action_ApplyReturnGoods);
		getDataQueue.setWhat(GetDataConfing.What_ApplyReturnGoods);
		//解析
		getDataQueue.setParamsNoJson(params);
		//网络数据回调
		getDataQueue.setCallBack(callBack);
		/**
		 * 进行网络请求
		 */
		getDataUtil.getData(getDataQueue);

	}

	private IGetServicesDataCallBack callBack = new IGetServicesDataCallBack() {

		@Override
		public void onLoaded(GetDataQueue entity) {
			xlv.stopLoadMore();
			xlv.stopRefresh();
			//判断网络请求数据返回是否成功
			if (entity.isOk()) {
				if (ShowGetDataError.isCodeSuccess(mContext, entity.getInfo())) {
//					if (entity.what == GetDataConfing.What_GetDiscover) {
//						ArrayList<JsonMap<String, Object>> temp_data = (ArrayList<JsonMap<String, Object>>) JsonParseHelper.getJsonMap_List_JsonMap(entity.getInfo(), JsonKeys.Key_Info);
//						setAdapterData(temp_data);
//						validateData();
//					}
				}
			} else {
				ShowGetDataError.showNetError(mContext);
			}
			loadingToast.dismiss();
		}

	};

	/**
	 * 校验数据 显示无数据等
	 * 是否请求正常 网络错误且数据为空的话显示网络异常界面 提供刷新的接口
	 *
	 * @param
	 */
	private void validateData() {
		if (datas == null || datas.size() == 0) {
			xlv.setVisibility(View.GONE);
			nodataLayout.setVisibility(View.VISIBLE);
		} else {
			xlv.setVisibility(View.VISIBLE);
			nodataLayout.setVisibility(View.GONE);
		}
	}

	private void setAdapterData(ArrayList<JsonMap<String, Object>> temp_data) {
		if (temp_data.size() == 0) {
			xlv.setPullLoadEnable(false);
		} else {
			xlv.setPullLoadEnable(true);
		}
		if (nPage == 0) {
			datas.clear();
			datas.addAll(0, temp_data);
		} else {
			datas.addAll(temp_data);
		}
		myAppointmentAdapter.setDatas(datas);
		myAppointmentAdapter.notifyDataSetChanged();
	}

	/**
	 * item   点击
	 */
	private AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

		}
	};

	/**
	 * 滑动加载更多，刷新监听
	 */
	private XListView.IXListViewListener loadMoreListener = new XListView.IXListViewListener() {
		@Override
		public void onRefresh() {
			getServerData();
		}


		@Override
		public void onLoadMore() {
			getServerData();
		}
	};
}